/** 
 * @desc 用户模块
 * @fileName userInfo.ts
 */
import { defineStore } from 'pinia'

type ObjType = {
  userId: string
  userName: string
  userPhone: string
  email?: string
  [key: string]: any
}

export const useUserInfoStore = defineStore({
  id: 'userInfo', // id唯一且必须
  state: () => {
    return {
      userInfo: {
        userId: '',
        userName: '',
        userPhone: ''
      },
      loginPhone: ''
    }
  },
  getters: {
    // 登录手机号脱敏展示
    loginPhoneShow () :string {
      return this.loginPhone.replace(/(\d{3})\d*(\d{4})/, '$1****$2')
    }
  },
  actions: {
    setUserInfo(payload: ObjType):void {
      this.userInfo = payload
    },
    setLoginPhone (payload: string):void {
      this.loginPhone = payload
    }
  },
  persist: {
    key: 'userInfo', // 持久化的key
    paths: ['userInfo.userId', 'loginPhone'], // 需要持久化的数据
    storage: sessionStorage // 默认localStorage
  }
})

export default useUserInfoStore
